//
//  ViewController.m
//  OrientationTest
//
//  Created by yao zhaofeng on 2018/6/20.
//  Copyright © 2018年 YaoZhaofeng. All rights reserved.
//

#import "ViewController.h"
#import "XYViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UIImageView *imgViewH;
@property (nonatomic, strong) UIImageView *imgViewV;
@property (nonatomic, assign) BOOL isLandscape;

@end
static int times = 0;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor redColor];
    _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
//    _webView.userInteractionEnabled = NO;
//    [self.view addSubview:_webView];
//    NSURL *url = [NSURL URLWithString:@"https://club.jj.cn/jjinfo.html"];
//    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
//    _webView.scalesPageToFit = YES;
//    [_webView loadRequest:urlRequest];
    
    _imgViewV = [[UIImageView alloc] initWithFrame:self.view.bounds];
    _imgViewV.image = [UIImage imageNamed:@"IMG_8042.PNG"];
    [self.view addSubview:_imgViewV];
    
    _imgViewH = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width)];
    _imgViewH.image = [UIImage imageNamed:@"IMG_8043.PNG"];
//    _imgViewH.hidden = YES;
    [self.view addSubview:_imgViewH];
    
    
    NSInteger size = [NSProcessInfo processInfo].physicalMemory/(1024*1024);
    NSLog(@"[NSProcessInfo processInfo].physicalMemory is %llu", [NSProcessInfo processInfo].physicalMemory);
    NSLog(@"内存: %ld", (long)size);
    NSLog(@"内存: %llu", [NSProcessInfo processInfo].physicalMemory/(1024*1024*1024));
}


- (void)viewWillLayoutSubviews {
//    [_webView setFrame:self.view.bounds];
//    [_imgViewH setFrame:self.view.bounds];
}

- (IBAction)btnClick:(id)sender {
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//横屏时，状态栏会消失
//解决方法：确保Info.plist中的【View controller-based status bar appearance】为YES，然后重写viewController的- (BOOL)prefersStatusBarHidden，返回值是NO
- (BOOL)prefersStatusBarHidden {
    return YES;
}

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if (self.isLandscape) {
        return UIInterfaceOrientationMaskLandscape;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}



- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    if (self.isLandscape) {
        return UIInterfaceOrientationLandscapeLeft;
    } else {
        return  UIInterfaceOrientationPortrait;
    }
}

- (void)setOritation {
    
    self.isLandscape = !_isLandscape;
    __weak ViewController *weakSelf = self;
    UIViewController *tempVC = [UIViewController new];
   tempVC.modalPresentationStyle = UIModalPresentationFullScreen;
   [self presentViewController:tempVC animated:NO completion:^{
       [weakSelf dismissViewControllerAnimated:NO completion:nil];
   }];
}

//- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//    return UIInterfaceOrientationMaskLandscape;
//}

/**
 *文档关于UIInterfaceOrientationLandscapeLeft的说明：The device is in landscape mode, with the device held upright and the Home button on the right side.
 *实际验证的时候发现，home应该是在左侧的，这块不知道是不是注释错误了~~
 *
 */
//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
//    return UIInterfaceOrientationLandscapeLeft;
//}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self setOritation];
//    XYViewController *XYVC = [[XYViewController alloc] init];
//    XYVC.modalPresentationStyle = UIModalPresentationFullScreen;
//    if (times%2 == 0) {
////        [self.navigationController pushViewController:XYVC animated:YES];
//        XYVC.isLandScape = YES;
//        [self presentViewController:XYVC animated:NO completion:nil];
//    } else {
//        [self presentViewController:XYVC animated:NO completion:nil];
//    }
//    times++;
    
//    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//    [self presentViewController:picker animated:NO completion:nil];
//    if (times % 2 == 0) {
//        self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
//        CGRect bounds = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
//        self.view.bounds = bounds;
//        _imgViewH.hidden = NO;
//    } else {
//        self.view.transform = CGAffineTransformMakeRotation(0);
//        CGRect bounds = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
//        self.view.bounds = bounds;
//        _imgViewH.hidden = YES;
//    }
//    times++;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
}

@end
